<?php
class ModelCatalogCat extends Model {
	public function getCat($cat_id) {
		$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "cat c LEFT JOIN " . DB_PREFIX . "cat_description cd ON (c.cat_id = cd.cat_id)  WHERE c.cat_id = '" . (int)$cat_id . "' AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND c.status = '1'");
		
		return $query->row;
	}
	
	public function countLayout($parent_id = 0,$count=0){
		if($this->getCats($parent_id)){
			$count++;
			$results=$this->getCats($parent_id);
			foreach ($results as $result){
				if($this->getCats($result["cat_id"])){
					$count=$this->countLayout($result["cat_id"],$count);
				}
			}
		}
		return $count;
	}
	
	public function checkChildrenMenu($parent_id = 0,$check=false){
		if($this->getCats($parent_id)){
		$results=$this->getCats($parent_id);
			foreach ($results as $result){
				if(!$this->getCats($result["cat_id"])){
					$check=true;
					return $check;
				}
			}
		}
		return $check;
	}
	
	public function getCats($parent_id = 0) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "cat c LEFT JOIN " . DB_PREFIX . "cat_description cd ON (c.cat_id = cd.cat_id)  WHERE c.parent_id = '" . (int)$parent_id . "' AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "'  AND c.status = '1' ORDER BY c.sort_order, LCASE(cd.name)");
		
		return $query->rows;
	}
				
	public function getTotalCatsByCatId($parent_id = 0) {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "cat as c WHERE c.parent_id = '" . (int)$parent_id . "' AND c.status = '1'");
		
		return $query->row['total'];
	}
}
?>